package com.thghh.util;

import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.usermodel.*;

/**
 * 读取Excel数据
 *
 * @author pengzhikang
 * @date 2021/12/07 18:00
 * @since 1.0.0
 */
public class ExcelDataUtils {


    /**
     * 获得值
     *
     * @param row    行
     * @param column 列
     * @return {@link Object }
     * @author pengzhikang
     * @date 2021/12/09 11:55
     */
    public static <T> T getValue(Row row, int column) {
        return getValue(row.getCell(column));
    }

    /**
     * 获得值
     *
     * @param row    行
     * @param column 列
     * @return {@link Object }
     * @author pengzhikang
     * @date 2021/12/09 11:55
     */
    public static String getStringValue(Row row, int column) {
        Object obj = getValue(row.getCell(column));
        return obj == null ? null : String.valueOf(obj);
    }

    /**
     * 获得值
     *
     * @param row    行
     * @param column 列
     * @return {@link Object }
     * @author pengzhikang
     * @date 2021/12/09 11:55
     */
    public static int getIntValue(Row row, int column) {
        Double obj = getValue(row.getCell(column));
        return obj == null ? 0 : obj.intValue();
    }

    /**
     * 解析单元格中的值
     *
     * @param cell 单元格
     * @return 单元格内的值
     */
    public static <T> T getValue(Cell cell) {
        if (null == cell) {
            return null;
        }
        Object value = null;
        switch (cell.getCellTypeEnum()) {
            case BOOLEAN:
                value = cell.getBooleanCellValue();
                break;

            case NUMERIC:
                // 日期类型，转换为日期
                if (DateUtil.isCellDateFormatted(cell)) {
                    value = cell.getDateCellValue();
                }
                // 数值类型
                else {
                    // 默认返回double，创建BigDecimal返回准确值
                    value = cell.getNumericCellValue();
                }
                break;
            case BLANK:
                value = null;
                break;
            case FORMULA:
                FormulaEvaluator evaluate = cell.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator();
                CellValue cellValue = evaluate.evaluate(cell);
                switch (cellValue.getCellTypeEnum()) {
                    case NUMERIC:
                        value = cellValue.getNumberValue();
                        break;
                    case STRING:
                        value = cellValue.getStringValue();
                        break;
                    case BOOLEAN:
                        value = cellValue.getBooleanValue();
                        break;
                    case ERROR:
                        value = ErrorEval.getText(cellValue.getErrorValue());
                        break;
                    default:
                        value = "";
                        break;
                }
                break;
            case STRING:
            default:
                value = cell.getStringCellValue();
                break;
        }

        return (T) value;
    }

}
